package ael.imarkket.Library;

import java.util.HashMap;
import java.util.Map;

import ael.imarkket.R;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class StatusApp {

	private static StatusApp	instance	= new StatusApp();
	static Context				context;
	ConnectivityManager			connectivityManager;
	NetworkInfo					wifiInfo, mobileInfo;
	boolean						connected	= false;

	public static StatusApp getInstance(Context ctx) {
		context = ctx;
		return instance;
	}

	public boolean isOnline(Context con) {
		try {
			connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);

			NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
			connected = networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
			return connected;

		} catch (Exception e) {
			Logging.getInstance().log(Logging.LOG_ERROR, "isOnline: " + e.toString());
		}
		return connected;
	}

	/**
	 * Get all connection details 1. Status 2. Type: Mobile/wifi 3. Sub type 4. Roaming status (Only for mobile network)
	 * 
	 * @return Map<String, String> http://www.compiletimeerror.com/2013/06/check- internet-connection-in-android.html#.Uibe2DaGG3N
	 */
	private Map<String, String> getConnectionDetails(Context con) {
		Map<String, String> networkDetails = new HashMap<String, String>();
		try {
			ConnectivityManager connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo wifiNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			if (wifiNetwork != null && wifiNetwork.isConnected()) {
				networkDetails.put("Type", wifiNetwork.getTypeName());
				networkDetails.put("Sub type", wifiNetwork.getSubtypeName());
				networkDetails.put("State", wifiNetwork.getState().name());
			}

			NetworkInfo mobileNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			if (mobileNetwork != null && mobileNetwork.isConnected()) {
				networkDetails.put("Type", mobileNetwork.getTypeName());
				networkDetails.put("Sub type", mobileNetwork.getSubtypeName());
				networkDetails.put("State", mobileNetwork.getState().name());
				if (mobileNetwork.isRoaming()) {
					networkDetails.put("Roming", "YES");
				} else {
					networkDetails.put("Roming", "NO");
				}
			}
		} catch (Exception e) {
			// networkDetails.put("Status", e.getMessage());
			Logging.getInstance().log(Logging.LOG_ERROR, "getConnectionDetails: " + e.toString());
		}
		return networkDetails;
	}

	// Determinamos el tipo de conexion
	public String getTypeStatus() {
		Map<String, String> networkDetails = getConnectionDetails(context);
		String salida = "networkDetails: ";

		if (networkDetails.isEmpty()) {
			salida += context.getString(R.string.AppStatus_empty);
		} else {
			if (networkDetails.containsKey("State")) {
				salida += "Status: " + networkDetails.get("State") + " ";
			}
			if (networkDetails.containsKey("Type")) {
				salida += "Type: " + networkDetails.get("Type") + " ";
			}
			if (networkDetails.containsKey("Sub type")) {
				salida += "SubType: " + networkDetails.get("Sub type") + " ";
			}
			if (networkDetails.containsKey("Roming")) {
				salida += "Roming: " + networkDetails.get("Roming") + " ";
			}
		}
		return salida;
	}
}